Skip to content

ext/bcmath: use vector in compare #18859

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

SakiTakamachi
Copy link
Member

@SakiTakamachi SakiTakamachi commented Jun 16, 2025

Benchmark

Since #18871 had a few issues, I've reopened the PR with this version, which is more balanced and stable.

Small value 1

for ($i = 0; $i < 10000000; $i++) {
    bccomp('1', '2', 0);
}
Benchmark 1: /php-dev2/sapi/cli/php /mount/bc/cmp/0_2.php
  Time (mean ± σ):     265.4 ms ±  13.8 ms    [User: 259.1 ms, System: 3.9 ms]
  Range (min … max):   250.9 ms … 290.7 ms    10 runs
 
Benchmark 2: /master/sapi/cli/php /mount/bc/cmp/0_2.php
  Time (mean ± σ):     259.7 ms ±   8.4 ms    [User: 253.3 ms, System: 3.9 ms]
  Range (min … max):   247.1 ms … 270.0 ms    11 runs
 
Summary
  '/master/sapi/cli/php /mount/bc/cmp/0_2.php' ran
    1.02 ± 0.06 times faster than '/php-dev2/sapi/cli/php /mount/bc/cmp/0_2.php'

Small value 2

for ($i = 0; $i < 10000000; $i++) {
    bccomp('1.2345', '1.23456', 5);
}
Benchmark 1: /php-dev2/sapi/cli/php /mount/bc/cmp/0.php
  Time (mean ± σ):     439.0 ms ±   2.6 ms    [User: 433.7 ms, System: 2.9 ms]
  Range (min … max):   434.4 ms … 444.2 ms    10 runs
 
Benchmark 2: /master/sapi/cli/php /mount/bc/cmp/0.php
  Time (mean ± σ):     443.4 ms ±   5.5 ms    [User: 437.4 ms, System: 3.5 ms]
  Range (min … max):   434.0 ms … 452.2 ms    10 runs
 
Summary
  '/php-dev2/sapi/cli/php /mount/bc/cmp/0.php' ran
    1.01 ± 0.01 times faster than '/master/sapi/cli/php /mount/bc/cmp/0.php'

Middle value 1

for ($i = 0; $i < 10000000; $i++) {
    bccomp('1.234567890123', '1.234567890123', 15);
}
Benchmark 1: /php-dev2/sapi/cli/php /mount/bc/cmp/1.php
  Time (mean ± σ):     475.9 ms ±   1.7 ms    [User: 469.5 ms, System: 3.9 ms]
  Range (min … max):   472.0 ms … 477.4 ms    10 runs
 
Benchmark 2: /master/sapi/cli/php /mount/bc/cmp/1.php
  Time (mean ± σ):     489.9 ms ±   4.3 ms    [User: 483.9 ms, System: 3.5 ms]
  Range (min … max):   485.0 ms … 497.7 ms    10 runs
 
Summary
  '/php-dev2/sapi/cli/php /mount/bc/cmp/1.php' ran
    1.03 ± 0.01 times faster than '/master/sapi/cli/php /mount/bc/cmp/1.php'

Middle value 2

for ($i = 0; $i < 10000000; $i++) {
    bccomp('1.23456789012345678901234567890123456789012345678901234567890123456789', '1.23456789012345678901234567890123456789012345678901234567890123456789', 40);
}
Benchmark 1: /php-dev2/sapi/cli/php /mount/bc/cmp/2.php
  Time (mean ± σ):     492.9 ms ±   3.0 ms    [User: 486.2 ms, System: 4.3 ms]
  Range (min … max):   487.9 ms … 498.1 ms    10 runs
 
Benchmark 2: /master/sapi/cli/php /mount/bc/cmp/2.php
  Time (mean ± σ):     590.1 ms ±   2.9 ms    [User: 584.2 ms, System: 3.4 ms]
  Range (min … max):   585.6 ms … 593.9 ms    10 runs
 
Summary
  '/php-dev2/sapi/cli/php /mount/bc/cmp/2.php' ran
    1.20 ± 0.01 times faster than '/master/sapi/cli/php /mount/bc/cmp/2.php'

Large value

for ($i = 0; $i < 100000; $i++) {
    bccomp(str_repeat('1234567890', 3000), str_repeat('1234567890', 3000), 0);
}
Benchmark 1: /php-dev2/sapi/cli/php /mount/bc/cmp/3.php
  Time (mean ± σ):     571.7 ms ±  22.2 ms    [User: 566.5 ms, System: 2.8 ms]
  Range (min … max):   561.6 ms … 634.5 ms    10 runs

Benchmark 2: /master/sapi/cli/php /mount/bc/cmp/3.php
  Time (mean ± σ):      1.322 s ±  0.002 s    [User: 1.316 s, System: 0.004 s]
  Range (min … max):    1.319 s …  1.326 s    10 runs
 
Summary
  '/php-dev2/sapi/cli/php /mount/bc/cmp/3.php' ran
    2.31 ± 0.09 times faster than '/master/sapi/cli/php /mount/bc/cmp/3.php'

@SakiTakamachi SakiTakamachi marked this pull request as ready for review June 16, 2025 13:38
@SakiTakamachi
Copy link
Member Author

Since using memcmp turned out to be faster, I will close this version.

@SakiTakamachi SakiTakamachi deleted the bcmath/use_vector_in_compare branch June 18, 2025 02:29
@SakiTakamachi SakiTakamachi restored the bcmath/use_vector_in_compare branch June 18, 2025 03:47
@SakiTakamachi SakiTakamachi reopened this Jun 19, 2025
@SakiTakamachi
Copy link
Member Author

@nielsdos
Sorry for the back and forth, but I'd like to propose this version instead 🙏

Copy link
Member

@nielsdos nielsdos left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks like a nice balance, good.

@nielsdos
Copy link
Member

nielsdos commented Jun 19, 2025

It would be nice if the code duplication (for the magnitude check & return value, i.e. lines 106-120) would be resolved by using a separate function to return the value, but it's not critical.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants